using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for VersionsControl.
	/// </summary>
	public class VersionsControl : AdminUserControl
	{
    private ExtendedDataGrid versionsGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public VersionsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list", new ActionExecuteDelegate(RefreshList));
      actionList.Add("Copy to clipboard", "Copy the list to the clipboard", new ActionExecuteDelegate(CopyToClipboard));
      actionList.Add("Save selected versions...", "Save the selected versions of the procedure/library", 
        new ActionExecuteDelegate(SaveSelected));
      actionList.Add("Delete selected versions", "Delete the selected versions of the procedure/library from the database", 
        new ActionExecuteDelegate(DeleteVersions));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.versionsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.versionsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // versionsGrid
      //
      this.versionsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.versionsGrid.Location = new System.Drawing.Point(0, 0);
      this.versionsGrid.Name = "versionsGrid";
      this.versionsGrid.Size = new System.Drawing.Size(474, 408);
      this.versionsGrid.TabIndex = 0;
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(474, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 408);
      this.uiActionsControl.TabIndex = 2;
      // 
      // VersionsControl
      // 
      this.Controls.Add(this.versionsGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "VersionsControl";
      this.Size = new System.Drawing.Size(624, 408);
      ((System.ComponentModel.ISupportInitialize)(this.versionsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private VersionCollection versions;
    public void LoadVersions(VersionCollection versions)
    {
      this.versions = versions;
      versionsGrid.DataSource = null;
      versionsGrid.DataSource = versions;
      versionsGrid.ClearColumns();
      versionsGrid.AddColumn("PublishedTime", "Published");
      versionsGrid.AddColumn("VersionNumber", "Version");
      versionsGrid.AddColumn("Owner");
      versionsGrid.AddColumn("Description");
      versionsGrid.AddColumn("Type", "File type");
      versionsGrid.AddColumn("EworkVersion", "Metastorm BPM version");
      versionsGrid.AddColumn("Size", "Size (in bytes)");
      versionsGrid.AutosizeColumns();
    }

    private string SaveSelected()
    {
      if (versionsGrid.CurrentRowIndex > -1)
      {
        int count = 0;
        CurrencyManager bm = (CurrencyManager)versionsGrid.BindingContext[versionsGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(versionsGrid.IsSelected(i))
          {
            count++;
          }
        }

        if (count > 1)
        {
          // folder dialog
          using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
          {
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
              using (new CursorHandler())
              {
                for (int i = 0; i < bm.Count; i++) 
                { 
                  if(versionsGrid.IsSelected(i))
                  {
                    FreeFlow.Administration.Version version = (FreeFlow.Administration.Version)bm.List[i];
                    version.SaveToFile(folderDialog.SelectedPath + "\\" + version.Name + version.VersionNumber + "." + version.Type);
                  }
                }
              }
            }
          }
        }
        else
        {
          // save dialog
          FreeFlow.Administration.Version version = (FreeFlow.Administration.Version)bm.Current;
          VersionControl.SaveVersion(version);
        }
      }
      return null;
    }

    private string DeleteVersions()
    {
      if (MessageBox.Show("Delete the selected versions?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          versionsGrid.DeleteSelected();
          OnRefreshRequired();
        }
      }
      return null;
    }

    private string RefreshList()
    {
      versions.Refresh();
      LoadVersions(versions);
      OnRefreshRequired();
      return null;
    }

    private string CopyToClipboard()
    {
      versionsGrid.CopyToClipboard();
      return null;
    }
	}
}
